package com.cskaoyan.controller.admin;

import com.cskaoyan.anno.LogAnnotation;
import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.AdminCreateBo;
import com.cskaoyan.bean.bo.AdminParam;
import com.cskaoyan.bean.pojo.MallAdmin;
import com.cskaoyan.bean.vo.AdminCreateVo;
import com.cskaoyan.bean.vo.AdminListVo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.util.Md5Util;
import com.fasterxml.jackson.databind.ser.Serializers;
import lombok.SneakyThrows;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author: Fengyi
 * @description: 管理员管理 list create delete update
 * @time: 2021/7/9 8:41
 */
@RequestMapping("admin/admin")
@RestController
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * @Author fengyi
     * @description: 分页查询管理员
     * @Date 20:38 2021/7/8
     * url:
     */
    @RequestMapping("list")
    @RequiresPermissions("admin:admin:list")
    public BaseRespVo adminList(AdminParam adminParam) {
        BaseData<MallAdmin> mallAdmin = adminService.queryAdmin(adminParam);
        return BaseRespVo.ok(mallAdmin);
    }

    /**
     * @Author fengyi
     * @description: 新建管理员
     * @Date 8:42 2021/7/9
     * url:
     */
    @SneakyThrows
    @LogAnnotation
    @RequestMapping("create")
    @RequiresPermissions("admin:admin:create")
    public BaseRespVo createAdmin(@Validated @RequestBody MallAdmin mallAdmin,
                                  BindingResult bindingResult) {

        //校验用户名，密码输入
        //用户名长度在 8 - 25
        //密码长度在 6 - 18
        //roleIds 长度不能是 0
        if(bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String message = fieldError.getDefaultMessage();
            return BaseRespVo.fail(message);
        }

        Date date = new Date();
        mallAdmin.setAddTime(date);
        mallAdmin.setUpdateTime(date);

        // 对密码来进行加密操作
        String password = mallAdmin.getPassword();
        String passwordMD5 = Md5Util.getMd5(password);
        mallAdmin.setPassword(passwordMD5);

        int code = adminService.addAdmin(mallAdmin);

        if(code == -1) {
            //有重名
            return BaseRespVo.fail("用户名重复");
        }
        mallAdmin.setId(code);

        return BaseRespVo.ok(mallAdmin);
    }

    /**
     * @Author fengyi
     * @description: 逻辑删除admin, updatetime
     * @Date 13:13 2021/7/9
     * url:
     */
    @LogAnnotation
    @RequestMapping("delete")
    @RequiresPermissions("admin:admin:delete")
    public BaseRespVo deleteAdmin(@RequestBody MallAdmin mallAdmin) {
        adminService.deleteAdmin(mallAdmin);
        return BaseRespVo.ok();
    }
    
    /**
     * @Author fengyi
     * @description: update
     * @Date 13:54 2021/7/9
     * url:        
     */
    @SneakyThrows
    @LogAnnotation
    @RequestMapping("update")
    @RequiresPermissions("admin:admin:update")
    public BaseRespVo updateAdmin(@RequestBody @Validated MallAdmin mallAdmin,
                                  BindingResult bindingResult) {

        if(bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String message = fieldError.getDefaultMessage();
            return BaseRespVo.fail(message);
        }

        Date date = new Date();
        String password = mallAdmin.getPassword();
        String passwordMD5 = Md5Util.getMd5(password);
        mallAdmin.setPassword(passwordMD5);
        mallAdmin.setUpdateTime(date);
        mallAdmin.setAddTime(null);

        int code = adminService.updateService(mallAdmin);
        if(code == 404) {
            //有重名
            return BaseRespVo.fail("用户名重复");
        }

        return BaseRespVo.ok(mallAdmin);
    }
}
